import Vue from 'vue'
import store from "../store/index.js"

// const API = 'https://schooltc.fengmiao.net/index.php' //正式
// const API = 'http://221.234.43.162:81/index.php'//测试

// const API = 'https://mini.fengmiao.net/index.php'//测试
// const BaseURL = 'https://mini.fengmiao.net'  // 图片服务器

const API = 'https://mini.fengmiao.net/index.php'//测试
const BaseURL = 'https://mini.fengmiao.net'  // 图片服务器



const qfapp = 'https://face.fengmiao.net' //人脸识别


// http://221.234.43.162:81/index.php/api/outboundmanage/getgoods

	
const quest = (url, method, data,header) => {
	
	var token = uni.getStorageSync("token")
	let httpDefaultOpts = {
		url: API + url,
		data: data,
		method: method,
		header: {
			'content-type': 'application/json',
			'token':  token,
			...header
		},
		dataType: 'json',
	}

	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				if(res[1].statusCode==200){
					if(res[1].data.code==200 || res[1].data.code==1  ){
						resolve(res[1].data)
						
					}else if(res[1].data.code==50007){
						// console.log('50007')
						
						// 重新登录 获取  code
						
						uni.showToast({
							icon: 'none',
							title: res[1].data.message , 
							duration: 1000
						});
						resolve(res[1].data)
						
					}else{
						
						resolve(res[1].data)
					}
				}else{
					uni.showToast({
						icon: 'none',
						title: res[1].data.message , 
						duration: 2000
					});
					reject(res[1].data)
				}
			}
		).catch(
			(response) => {
				uni.showToast({
					title: '网络连接失败',
					icon: 'none'
				})
				reject(response)
			}
		)
	})
	return promise
};

// token 
const questToken = (url, method, data) => {
	let httpDefaultOpts = {
		url: API + url,
		data:  Object.assign(data, {token:uni.getStorageSync('token')}),
		method: method,
		header: {
			"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
		},
		dataType: 'json',
	}

	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				if  (res[1].data.code == 1) {
					resolve(res[1].data)
				} else if (res[1].data.code == 10) {
					uni.showToast({
						title: res[1].data.resule,
						icon: 'none',
						duration: 2000
					})
					setTimeout(()=>{uni.reLaunch({url:'/pages/login/login'})},800)
				} else {
					uni.showToast({
						title: res[1].data.resule,
						icon: 'none',
						duration: 2000
					})
					reject(res[1].data)
				}
			} 
		).catch(
			(response) => {
				// console.log(response)
				uni.showToast({
					title: '服务器繁忙,请稍后再试',
					icon: 'none'
				})
				reject(response)
			}
		)
	})
	return promise
};

export default {
	quest,
	API,
	questToken,
	BaseURL,
	qfapp
}


